package com.baseproject.widget;

import android.app.Dialog;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by hp on 2017/5/3.
 */

public class DialogUtils {

    Dialog mDialog;

    WindowManager.LayoutParams mLayoutParams;

    public DialogUtils(Dialog dialog) {
        mDialog = dialog;
    }

    public DialogUtils setContentView(View view) {
        mDialog.setContentView(view);
        return this;
    }

    public DialogUtils requestFeature(int feature) {
        mDialog.getWindow().requestFeature(feature);
        return this;
    }

    public DialogUtils addFlags(int flag) {
        mDialog.getWindow().addFlags(flag);
        return this;
    }

    private WindowManager.LayoutParams getLayoutParams() {
        if(mLayoutParams == null) {
            mLayoutParams = mDialog.getWindow().getAttributes();
        }
        return mLayoutParams;
    }

    public DialogUtils setGravity(int gravity) {
        getLayoutParams().gravity = gravity;
        return this;
    }

    public DialogUtils setWidth(int width) {
        getLayoutParams().width = width;
        return this;
    }

    public DialogUtils setHeight(int height) {
        getLayoutParams().height = height;
        return this;
    }

    public DialogUtils setDimAmount(float dimAmount) {
        getLayoutParams().dimAmount = dimAmount;
        return this;
    }

    public DialogUtils setType(int type) {
        getLayoutParams().type = type;
        return this;
    }

    public DialogUtils setFormat(int format) {
        getLayoutParams().format = format;
        return this;
    }

    public DialogUtils setWindowAnimations(int windowAnimations) {
        getLayoutParams().windowAnimations = windowAnimations;
        return this;
    }

    public DialogUtils setCancelable(boolean cancelable) {
        mDialog.setCancelable(cancelable);
        return this;
    }

    public DialogUtils setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
        mDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        return this;
    }

    public DialogUtils setBackgroundDrawableResource(int backgroundDrawableResource) {
        mDialog.getWindow().setBackgroundDrawableResource(backgroundDrawableResource);
        return this;
    }

    public Dialog getDialog() {
        return mDialog;
    }

    public void show() {
        mDialog.show();
    }

}
